﻿function showDSProblemList(eid) {
	if (refreshTab('showDSProblemList', eid)) return;
	var showPreview = window.screen.width > 800;

	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : url.getDSProblemList
						}),
				reader : new Ext.data.JsonReader({
							root : 'result',
							totalProperty : 'total',
							id : 'pid'
						}, ['eid', 'pid', 'title', 'description', 'accept', 'submit', 'status'])
			});
	store.load({
				params : {
					eid : eid
				}
			});

	var cm = new Ext.grid.ColumnModel([{
				header : 'Problem Title',
				sortable : false,
				dataIndex : 'title',
				renderer : titleFormat
			}, {
				header : 'Statistics',
				sortable : false,
				dataIndex : 'submit',
				width : 30,
				renderer : ratioFormat
			}]);

	var sm = new Ext.grid.RowSelectionModel({
				singleSelect : true
			});

	var toolbar = new Ext.Toolbar({
				items : [new Ext.Button({
									text : '刷新',
									iconCls : 'refresh-icon',
									handler : function() {
										store.reload();
									}
								}), new Ext.Button({
									text : '预览',
									iconCls : 'preview-icon',
									enableToggle : true,
									pressed : showPreview,
									toggleHandler : function(sender, pressed) {
										showPreview = pressed;
										grid.view.refresh();
									}
								}), new Ext.Toolbar.Separator(), new Ext.Button({
									text : '实验状态',
									iconCls : 'status-icon',
									disabled : true,
									handler : function() {
										showDSStatusList({
													page : 1,
													eid : eid
												});
									}
								}), new Ext.Button({
									text : '查看题目',
									iconCls : 'problemlist-icon',
									disabled : true,
									handler : onShowProblem
								})]
			});

	var godbuttons;
	if (sys.god || sys.teacher) {
		godbuttons = [new Ext.Toolbar.Separator(), new Ext.Button({
							text : '添加题目',
							iconCls : 'add-icon',
							handler : function() {
								addOrEditDSProblem({
											eid : eid
										});
							}
						}), new Ext.Button({
							text : '编辑题目',
							iconCls : 'edit-icon',
							handler : onEdit,
							disabled : true
						}), new Ext.Button({
							text : '删除题目',
							iconCls : 'delete-icon',
							handler : onDelete,
							disabled : true
						}), new Ext.Toolbar.Separator(), new Ext.Button({
							text : '重判代码',
							iconCls : 'rejudge-icon',
							handler : onRejudgeStatus,
							disabled : true
						}), new Ext.Button({
							text : '删除代码',
							iconCls : 'clean-icon',
							handler : onCleanStatus,
							disabled : true
						}), new Ext.Toolbar.Separator(), new Ext.Button({
							text : '上移',
							iconCls : 'up-icon',
							disabled : true,
							handler : onUpProblem
						}), new Ext.Button({
							text : '下移',
							iconCls : 'down-icon',
							disabled : true,
							handler : onDownProblem
						})];
		toolbar.add(godbuttons);
	}

	toolbar.add([new Ext.Toolbar.Fill(), new Ext.Button({
						text : '上传实验报告',
						iconCls : 'report-icon',
						handler : function() {
							uploadReport(eid);
						}
					})]);

	var grid = new Ext.grid.GridPanel({
				id : 'showDSProblemList',
				title : 'Experiment',
				iconCls : 'problemlist-icon',
				closable : true,
				region : 'center',
				loadMask : {
					msg : '载入中...'
				},
				viewConfig : {
					forceFit : true,
					enableRowBody : true,
					showPreview : showPreview,
					getRowClass : previewFormat,
					scrollToRow : function(rowIndex) {
						var firstRow = Ext.get(this.getRow(0));
						var row = Ext.get(this.getRow(rowIndex));
						var distance = row.getOffsetsTo(firstRow)[1];
						this.scroller.dom.scrollTop = distance;
					}
				},
				store : store,
				cm : cm,
				sm : sm,
				tbar : toolbar
			});
	sm.on('selectionchange', onSelect);
	grid.on('rowdblclick', onShowProblem);
	grid.onRefresh = onRefresh;

	addTab(grid);

	function onRefresh(neweid) {
		if (Ext.isNumber(neweid)) eid = neweid;
		store.reload({
					params : {
						eid : eid
					}
				});
	}
	function titleFormat(value, p, record) {
		if (showPreview)
			return String
					.format(
							'<div class="{2}" style="height:16px">{0}&nbsp; '
									+ '<a href="javascript:void(0);" onclick="showDSProblem({pid:{4},eid:{3}});" class="likelink" style="color:black;font-weight:bold">{1}</a>'
									+ '<br />', int2char(record.data.pid), value, ['problem-icon',
									'solving-icon', 'accepted-icon'][record.data.status], eid,
							record.data.pid);
		return String
				.format(
						'<div class="{2}" style="height:16px">{0}&nbsp; '
								+ '<a href="javascript:void(0);" onclick="showDSProblem({pid:{4},eid:{3}});" class="likelink" style="color:black;font-weight:bold">{1}</a>',
						int2char(record.data.pid), value, ['problem-icon', 'solving-icon',
								'accepted-icon'][record.data.status], eid, record.data.pid);
	}
	function ratioFormat(value, p, record) {
		if (showPreview)
			return String.format(
					'{0} / {1}<br /><div>(<span class="ratio-text"> {2} </span>)</div>',
					record.data.accept, value, sys.rd.ratio(record.data.accept, value));
		return String.format('{0} / {1}&nbsp; (<span class="ratio-text"> {2} </span>)',
				record.data.accept, value, sys.rd.ratio(record.data.accept, value));
	}
	function previewFormat(record, rowIndex, p) {
		if (showPreview) {
			p.body = String.format('<p>{0}</p>', record.data.description
							.gbtrim(window.screen.width > 800 ? 120 : 60));
			return 'x-grid3-row-expanded';
		}
		return 'x-grid3-row-collapsed';
	}

	function onEdit() {
		var select = getSelect();
		if (!select) return;
		addOrEditDSProblem({
					pid : select.data.pid,
					eid : select.data.eid
				});
	}
	function onDelete() {
		var select = getSelect();
		if (!select) return;
		deleteDSProblem({
					pid : select.data.pid,
					eid : select.data.eid,
					title : select.data.title
				});
	}
	function onRejudgeStatus() {
		var select = getSelect();
		if (!select) return;
		rejudgeDSProblemStatus({
					pid : select.data.pid,
					eid : select.data.eid,
					title : select.data.title
				});
	}
	function onCleanStatus() {
		var select = getSelect();
		if (!select) return;
		cleanDSProblemStatus({
					pid : select.data.pid,
					eid : select.data.eid,
					title : select.data.title
				});
	}

	function getSelect() {
		if (sm.getCount() == 0) return null;
		return sm.getSelected();
	}
	function onSelect() {
		var record = getSelect();
		var disable = !record;
		toolbar.get(3).setDisabled(disable);
		toolbar.get(4).setDisabled(disable);

		if (sys.god || sys.teacher) {
			godbuttons[2].setDisabled(disable);
			godbuttons[3].setDisabled(disable);
			godbuttons[5].setDisabled(disable);
			godbuttons[6].setDisabled(disable);
			godbuttons[8].setDisabled(disable);
			godbuttons[9].setDisabled(disable);
		}
	}
	function onShowProblem() {
		var record = getSelect();
		if (!record) return;
		showDSProblem({
					pid : record.data.pid,
					eid : record.data.eid
				});
	}
	function onUpProblem() {
		var record = getSelect();
		var index = store.indexOf(record)
		if (!record) return;
		if (index == 0) return;
		var last = store.getAt(index - 1);
		record.data.pid ^= last.data.pid;
		last.data.pid ^= record.data.pid;
		record.data.pid ^= last.data.pid;
		store.sort('pid', 'ASC');
		ajax({
					url : url.swapProblem,
					params : {
						flags : true,
						pid : last.data.pid,
						eid : eid
					},
					success : function() {
						store.reload();
					}
				})
	}
	function onDownProblem() {
		var record = getSelect();
		if (!record) return;
		var index = store.indexOf(record);
		if (index == store.getCount() - 1) return;
		var next = store.getAt(index + 1);
		record.data.pid ^= next.data.pid;
		next.data.pid ^= record.data.pid;
		record.data.pid ^= next.data.pid;
		store.sort('pid', 'ASC');
		ajax({
					url : url.swapProblem,
					params : {
						flags : false,
						pid : next.data.pid,
						eid : eid
					},
					success : function() {
						store.reload();
					}
				})
	}
}

function refreshDSProblemList(eid) {
	refreshTab('showDSProblemList' + eid);
}

function deleteDSProblem(obj) {
	confirmx('您确定要删除实验题目 '
					+ (obj.title ? '<br />(e' + obj.eid + ') ' + int2char(obj.pid) + ' - '
							+ obj.title : '(e' + obj.eid + ') ' + int2char(obj.pid))
					+ ' 吗？<br />该题的所有状态也会被一并删除！', function() {
				ajax({
							url : url.deleteDSProblem,
							params : {
								eid : obj.eid,
								pid : obj.pid
							},
							success : function() {
								refreshDSProblemList(obj.eid);
								if (obj.success) obj.success();
							}
						});
			});
}

function rejudgeDSProblemStatus(obj) {
	confirmx('您确定要重判实验题目 '
					+ (obj.title ? '<br />(e' + obj.eid + ') ' + int2char(obj.pid) + ' - '
							+ obj.title : '(e' + obj.eid + ') ' + int2char(obj.pid))
					+ '<br />的所有代码吗？<br />该题的所有统计都会被重计。', function() {
				ajax({
							url : url.rejudgeDSProblemStatus,
							params : {
								eid : obj.eid,
								pid : obj.pid
							},
							success : function() {
								alertx('服务器接受了请求，代码正在重判...');
								refreshDSProblemList(obj.eid);
								showDSStatusList({
											eid : obj.eid,
											pid : obj.pid
										});
								if (obj.success) obj.success();
							}
						});
			});
}

function cleanDSProblemStatus(obj) {
	confirmx('您确定要清除实验题目 '
					+ (obj.title ? '<br />(e' + obj.eid + ') ' + int2char(obj.pid) + ' - '
							+ obj.title : '(e' + obj.eid + ') ' + int2char(obj.pid))
					+ '<br />的所有状态吗？<br />该题的所有统计都会被重置。', function() {
				ajax({
							url : url.cleanDSProblemStatus,
							params : {
								eid : obj.eid,
								pid : obj.pid
							},
							success : function() {
								alertx('状态已全部清除。');
								refreshDSProblemList(obj.eid);
								if (obj.success) obj.success();
							}
						});
			});
}
